<?php
if (!defined('DCS_ROOT')) exit('Access Denied'); 

class Ads_Model {

	var $DB;

	function __construct ($DB) {
		$this->DB = $DB;
		$this->tblName = "ads";
	} 

	function getOne($id) {
		$sql = "SELECT * FROM ads WHERE id = '$id' ";
		$this->DB->query($sql);
		return $this->DB->fetchRow();
	} 

	function getList($condition) {
		$sql = "SELECT * FROM ads ";
		$tmp = "";
		if (!empty($condition['condition'])) {
			$tmp .= "WHERE {$condition['condition']} ";
		} 
		if (!empty($condition['order'])) {
			$tmp .= " ORDER BY {$condition['order']} DESC ";
		} 
		if (!empty($condition['page']) && !empty($condition['rows'])) {
			$start = ($condition['page'] - 1) * $condition['rows'];
			$tmp .= "LIMIT " . $start . ", " . $condition['rows'];
		} 
		$sql .= $tmp;
		$this->DB->query($sql);
		return $this->DB->fetchAll();
	}

	function total($condition) {
		$sql = "SELECT count(*) AS total FROM ads ";
		$tmp = "";
		if (!empty($condition['condition'])) {
			$tmp .= "WHERE {$condition['condition']} ";
		} 
		if (!empty($condition['order'])) {
			$tmp .= " ORDER BY {$condition['order']} DESC ";
		} 
		$sql .= $tmp;
		$this->DB->query($sql);
		$this->DB->fetchRow();
		return $this->DB->getValue("total");
	}
	
	function addOne($val){
		$sql = "INSERT INTO ads (";
		foreach($val as $k => $v ){
			$tmp .= "$k,";
			$value .= " '$v',";
		}
		$sql .= rtrim($tmp,",").')  VALUES ('.rtrim($value,",").')';
		$this->DB->query($sql);
		return $this->DB->affectedRows();
	}
	
	function editOne($id,$val) {
		$sql = " UPDATE ads SET ";
		foreach($val as $k => $v ){
			$tmp .= " $k = '$v',";
		}
		$sql .= rtrim($tmp,",")." WHERE id = '$id' ";
		//de($sql);
		$this->DB->query($sql);
		return $this->DB->affectedRows();
	}

	function delOne($id) {
		$sql = "DELETE FROM ads WHERE id = '$id'" ;
		$this->DB->query($sql);
		return $this->DB->affectedRows();
	}

} 

?>
